package com.muyu.system.service.impl;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.DanmoAddresses;
import com.muyu.remote.RemoteAddressesService;
import com.muyu.system.domain.DanmoMarketOrder;
import com.muyu.system.mapper.DanmoMarketOrderMapper;
import com.muyu.system.service.IDanmoMarketOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 用户订单Service业务层处理
 *
 * @author yangle
 * @date 2024-09-08
 */
@Service
public class DanmoMarketOrderServiceImpl implements IDanmoMarketOrderService
{
    @Autowired
    private DanmoMarketOrderMapper danmoMarketOrderMapper;

    /**
     * 查询用户订单
     *
     * @param id 用户订单主键
     * @return 用户订单
     */
    @Override
    public DanmoMarketOrder selectDanmoMarketOrderById(Long id)
    {
        return danmoMarketOrderMapper.selectDanmoMarketOrderById(id);
    }

    /**
     * 查询用户订单列表
     *
     * @param danmoMarketOrder 用户订单
     * @return 用户订单
     */
    @Override
    public List<DanmoMarketOrder> selectDanmoMarketOrderList(DanmoMarketOrder danmoMarketOrder)
    {
        return danmoMarketOrderMapper.selectDanmoMarketOrderList(danmoMarketOrder);
    }

    /**
     * 新增用户订单
     *
     * @param danmoMarketOrder 用户订单
     * @return 结果
     */
    @Override
    public int insertDanmoMarketOrder(DanmoMarketOrder danmoMarketOrder)
    {
        danmoMarketOrder.setCreateTime(DateUtils.getNowDate());
        return danmoMarketOrderMapper.insertDanmoMarketOrder(danmoMarketOrder);
    }

    /**
     * 修改用户订单
     *
     * @param danmoMarketOrder 用户订单
     * @return 结果
     */
    @Override
    public int updateDanmoMarketOrder(DanmoMarketOrder danmoMarketOrder)
    {
        danmoMarketOrder.setUpdateTime(DateUtils.getNowDate());
        return danmoMarketOrderMapper.updateDanmoMarketOrder(danmoMarketOrder);
    }

    /**
     * 批量删除用户订单
     *
     * @param ids 需要删除的用户订单主键
     * @return 结果
     */
    @Override
    public int deleteDanmoMarketOrderByIds(Long[] ids)
    {
        return danmoMarketOrderMapper.deleteDanmoMarketOrderByIds(ids);
    }

    /**
     * 删除用户订单信息
     *
     * @param id 用户订单主键
     * @return 结果
     */
    @Override
    public int deleteDanmoMarketOrderById(Long id)
    {
        return danmoMarketOrderMapper.deleteDanmoMarketOrderById(id);
    }

    @Autowired
    private RemoteAddressesService remoteAddressesService;
    @Override
    public void addOrder(DanmoMarketOrder danmoMarketOrder) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        Long userid = loginUser.getUserid();
        DanmoAddresses danmoAddresses = new DanmoAddresses();
//         danmoAddresses.setUserId(userid);
         danmoAddresses.setUserId(Long.valueOf("1"));
        Result result = remoteAddressesService.list(danmoAddresses);
        Map<String, Object> data = (Map<String, Object>) result.getData();
        List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
        Optional<Map<String, Object>> itemWithId1 = list.stream()
                .filter(item -> {
                    Integer id = (Integer) item.get("id");
                    return id != null && id == 1;
                })
                .findFirst();
//        DanmoMarketOrder danmoMarketOrder1 = DanmoMarketOrder.builder()
//                .orderTime(DateUtils.getNowDate())
//                .userId(Long.valueOf("1"))
//                .addressesId((Integer) itemWithId1.get().get("id"))
//                .orderStatus(Long.valueOf(2))
//                .totalAmount(danmoMarketOrder.getTotalAmount())
//                .paymentMethod(Long.valueOf(1))
//                .totalAmount(danmoAddresses.get)
//                .build();
//        System.out.println(danmoMarketOrder1);
//        danmoMarketOrderMapper.insertDanmoMarketOrder(danmoMarketOrder1);
    }
}
